var $fileContent;
var $search;
var $tableFirstColumn;
var $tableOfEachRow;
var $downloadButton;
var $confirm_download;
var $cancelButton;
var $confirmButton;
var $downloadPanel;
var $whichButton;

$(function(){
	$fileContent = $(".fileDisplay .content");
	$search = $(" #search");
	$tableOfEachRow = $("table tr:not(:first)");
	$tableFirstColumn = $tableOfEachRow.find("td:first");
	$downloadButton = $(" table button.btn:contains('点我下载')");
	$confirm_download = $(".confirm-download");
	$cancelButton = $(".confirm-download input[type=button]");
	$confirmButton = $(".confirm-download input[type=submit]")
	$downloadPanel = $(" .confirm-download .download");

	
	setFileDisplayScroll();
	
	setSearchFocusListener();
	
	setDownloadButtonListener();
});

function setFileDisplayScroll(){
	$fileContent.mCustomScrollbar({
		theme:"white",
		scrollButtons:{
			enable:false
		},
		autoHideScrollbar:true,
		scrollInertia:100,
		horizontalScroll:false,
	});
}


function setSearchFocusListener(){
	$search.focus(function(){
		$search.velocity({width:"180px"}, 200, "ease");
		addSearchListener();
	});
	$search.blur(function(){
		$search.velocity({width:"100px"}, 200, "ease")
	});
}

function addSearchListener(){
	$search.on('input', function(){
		for(var i = 0; i < $tableFirstColumn.length; i++){
			var $oneTd = $tableFirstColumn.get(i).innerHTML.toLowerCase();
			while($oneTd.indexOf(" ") != -1){
				$oneTd = $oneTd.replace(" ", "");
			}
			if($oneTd.search($search.val().toLowerCase()) != -1){
				$tableOfEachRow.get(i).style.display = "block";
			}else{
				$tableOfEachRow.get(i).style.display = "none";
			}
			var $array = $search.val().toLowerCase().split(" ");
			for(var j = 0; j < $array.length; j++){
				if($oneTd.search($array[j]) != -1){
					if($array[j] != ""){
						$tableOfEachRow.get(i).style.display = "block";	
					}
				}				
			}
		}
	});
};


function setDownloadButtonListener(){
	
	$downloadButton.click(function(){
		//更改标题名称
		$titleName = $(this).parent().parents("tr").find("td").first().text();
		$confirm_download.find("p.title").text($titleName);
		
		$confirm_download.css({opacity:"0", display:"block"});
		$confirm_download.velocity({opacity:"1"}, 200);
		$whichButton = $(this);
	});
	
	$("#testtest").click(function(){
            
                alert($(this).parent().find("input").val());
//		if(isValid != null){
//			$("#testtest").html("下载");
//			$whichButton.parent().parent().html($(".current-user span").html());
//                        $confirm_download.css({display:"block"});
//                        $confirm_download.animate({}, 200)
                        $confirm_download.show();
//			$confirm_download.velocity({opacity:"1"}, 200, function(){
////				$confirm_download.css({display:"none"});
////			});
//			移除事件
			$downloadButton.unbind("click");
			//重新设置可借阅按钮
			$downloadButton = $(" table button:contains('点我下载')");
			//重新绑定事件
			$downloadButton.click(function(){
				$confirm_download.css({opacity:"0", display:"block"});
				$confirm_download.velocity({opacity:"1"}, 200);
				$whichButton = $(this);
			});
//		}		
	});
		

	$cancelButton.click(function(){
		$confirm_download.velocity({opacity:"0"}, 200, function(){
			$confirm_download.css({display:"none"});
		});
	});
	$confirm_download.click(function(){
		$confirm_download.velocity({opacity:"0"}, 200, function(){
			$confirm_download.css({display:"none"});
		});
	});
	$downloadPanel.click(function(){
		return false;
	});
	
	
}